//
//  GFLineViewController.m
//  GoodFood
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFLineViewController.h"
#define MYBUNDLE_NAME @ "mapapi.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]

@interface RouteAnnotation : BMKPointAnnotation


@property (nonatomic) int type;
@property (nonatomic) int degree;
@end

@implementation RouteAnnotation

@end


@interface GFLineViewController ()<BMKMapViewDelegate, BMKLocationServiceDelegate, BMKRouteSearchDelegate>
@property (nonatomic, assign) double lat;//餐厅纬度
@property (nonatomic, assign) double lon;//餐厅经度
@property (nonatomic, assign) double myLat;//我的纬度
@property (nonatomic, assign) double myLon;//我的经度
@property (nonatomic, copy) NSString *placeName;
@property (nonatomic, strong) BMKMapView *mapView;
@property (nonatomic, strong) BMKLocationService *locationService;
@property (nonatomic, strong) BMKRouteSearch *routeSearch;
@property (nonatomic, strong) UIButton *busButton;
@property (nonatomic, strong) UIButton *carButton;
@property (nonatomic, strong) UIButton *walkButton;
@property (nonatomic, assign) NSInteger busPlan;
@property (nonatomic, assign) NSInteger carPlan;
@property (nonatomic, assign) NSInteger walkPlan;
@property (nonatomic, copy) NSString *city;
@end

@implementation GFLineViewController
#pragma mark - 初始化
- (instancetype)initWithLat:(double)lat withLon:(double)lon withPlaceName:(NSString *)name withCity:(NSString *)city {
    self = [super init];
    if (self) {
        self.city = city;
        self.placeName = name;
        self.lat = lat;
        self.lon = lon;
    }
    return self;
}
#pragma mark - 创建button
- (void)creatButton {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    self.busButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.busButton];
    self.busButton.frame = CGRectMake(5, view.frame.size.height + 5, (ScreenWidth - 20) / 3 , ScreenHeight / 15);
    [self.busButton setTitle:@"公交" forState:0];
    [self.busButton setTintColor:[UIColor lightGrayColor]];
    self.busButton.layer.masksToBounds = YES;
    self.busButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.busButton.layer.borderWidth = 0.3;
    self.busButton.layer.cornerRadius = 5;
    [self.busButton addTarget:self action:@selector(bus:) forControlEvents:UIControlEventTouchUpInside];
    
    self.carButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.carButton];
    self.carButton.frame = CGRectMake(10 + (ScreenWidth - 20) / 3, view.frame.size.height + 5, (ScreenWidth - 20) / 3, ScreenHeight / 15);
    [self.carButton setTitle:@"自驾" forState:0];
    [self.carButton setTintColor:[UIColor lightGrayColor]];
    self.carButton.layer.masksToBounds = YES;
    self.carButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.carButton.layer.borderWidth = 0.3;
    self.carButton.layer.cornerRadius = 5;
    [self.carButton addTarget:self action:@selector(car:) forControlEvents:UIControlEventTouchUpInside];
    
    self.walkButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.walkButton];
    self.walkButton.frame = CGRectMake(15 + (ScreenWidth - 20) / 3 * 2, view.frame.size.height + 5, (ScreenWidth - 20) / 3, ScreenHeight / 15);
    [self.walkButton setTitle:@"步行" forState:0];
    [self.walkButton setTintColor:[UIColor lightGrayColor]];
    self.walkButton.layer.masksToBounds = YES;
    self.walkButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.walkButton.layer.borderWidth = 0.3;
    self.walkButton.layer.cornerRadius = 5;
    [self.walkButton addTarget:self action:@selector(walk:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 公交按钮点击
- (void)bus:(UIButton *)button {
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.pt = CLLocationCoordinate2DMake(self.myLat, self.myLon);
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    end.pt = CLLocationCoordinate2DMake(self.lat, self.lon);
    
    BMKTransitRoutePlanOption *transitRouteSearchOption = [[BMKTransitRoutePlanOption alloc]init];
    transitRouteSearchOption.city= self.city;
    transitRouteSearchOption.from = start;
    transitRouteSearchOption.to = end;
    
    BOOL flag = [self.routeSearch transitSearch:transitRouteSearchOption];
    
    if(flag)
    {
        NSLog(@"bus检索发送成功");
    }
    else
    {
        NSLog(@"bus检索发送失败");
    }
}
#pragma mark - 自驾按钮点击
- (void)car:(UIButton *)button {
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
   start.pt = CLLocationCoordinate2DMake(self.myLat, self.myLon);
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    end.pt = CLLocationCoordinate2DMake(self.lat, self.lon);
    
    BMKDrivingRoutePlanOption *drivingRouteSearchOption = [[BMKDrivingRoutePlanOption alloc]init];
    drivingRouteSearchOption.from = start;
    drivingRouteSearchOption.to = end;
    BOOL flag = [self.routeSearch drivingSearch:drivingRouteSearchOption];
    if(flag)
    {
        NSLog(@"car检索发送成功");
    }
    else
    {
        NSLog(@"car检索发送失败");
    }
}
#pragma mark - 步行按钮点击
- (void)walk:(UIButton *) button {
    BMKPlanNode* start = [[BMKPlanNode alloc]init];
    start.pt = CLLocationCoordinate2DMake(self.myLat, self.myLon);
    BMKPlanNode* end = [[BMKPlanNode alloc]init];
    end.pt = CLLocationCoordinate2DMake(self.lat, self.lon);

    
    
    BMKWalkingRoutePlanOption *walkingRouteSearchOption = [[BMKWalkingRoutePlanOption alloc]init];
    walkingRouteSearchOption.from = start;
    walkingRouteSearchOption.to = end;
    BOOL flag = [self.routeSearch walkingSearch:walkingRouteSearchOption];
    if(flag)
    {
        NSLog(@"walk检索发送成功");
    }
    else
    {
        NSLog(@"walk检索发送失败");
    }
}
#pragma mrak - 创建地图
- (void)creatMap {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    self.mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, view.frame.size.height + 10 +ScreenHeight / 15, ScreenWidth, ScreenHeight)];
    [self.view addSubview:self.mapView];
    [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(self.lat, self.lon)];
    [self.mapView setZoomLevel:13];
    self.locationService = [[BMKLocationService alloc] init];
    self.routeSearch = [[BMKRouteSearch alloc] init];
    //定位
    [self.locationService startUserLocationService];
    self.mapView.showsUserLocation = NO;
    self.mapView.userTrackingMode = BMKUserTrackingModeNone;
    self.mapView.showsUserLocation = YES;
}
#pragma mark - 导航
- (void)viewForNavi {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    view.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, ScreenHeight / 3, ScreenWidth / 3, ScreenHeight / 4)];
    label.text = @"路线";
    label.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    self.view.backgroundColor = [UIColor lightGrayColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewForNavi];
    [self creatMap];
    [self annotation];
    [self creatButton];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}
#pragma mark - 大头针信息
- (void)annotation {
    NSMutableArray *annotations = [NSMutableArray array];
    BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(self.lat,self.lon);
    item.coordinate = pt;
    item.title = self.placeName;
    [annotations addObject:item];
    [self.mapView addAnnotations:annotations];
    [self.mapView showAnnotations:annotations animated:1];
}
#pragma mark - 定位代理
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation {
    
    if (userLocation.location.coordinate.latitude != 0 && userLocation.location.coordinate.longitude != 0) {
        self.myLat = userLocation.location.coordinate.latitude;
        self.myLon = userLocation.location.coordinate.longitude;
        [self.mapView updateLocationData:userLocation];
        [self.locationService stopUserLocationService];
    }
}

///**
// *用户方向更新后，会调用此函数
// *@param userLocation 新的用户位置
// */
//- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
//{
//    
//    if (userLocation.location.coordinate.latitude != 0 && userLocation.location.coordinate.longitude != 0) {
//        [self.mapView updateLocationData:userLocation];
//        NSLog(@"heading is %@",userLocation.heading);
//    }
//
//    
//    
//}

#pragma mark - 定位结束
- (void)didStopLocatingUser {
    
    self.locationService.delegate = nil;
}
#pragma mark - 画大头针 根据确定的位置信息画出图片
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[RouteAnnotation class]]) {
        
        return [self getRouteAnnotationView:view viewForAnnotation:(RouteAnnotation*)annotation];
       
    } else {
        // 生成重用标示identifier
        NSString *AnnotationViewID = @"addressMark";
        
        // 检查是否有重用的缓存
        BMKAnnotationView* annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
        
        // 缓存没有命中，自己构造一个，一般首次添加annotation代码会运行到此处
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
            ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
            // 设置重天上掉下的效果(annotation)
            ((BMKPinAnnotationView*)annotationView).animatesDrop = NO;
        }
        
        // 设置位置
        annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
        annotationView.annotation = annotation;
        // 单击弹出泡泡，弹出泡泡前提annotation必须实现title属性
        annotationView.canShowCallout = YES;
        // 设置是否可以拖拽
        annotationView.draggable = NO;
        
        return annotationView;

    }

    
    
}
#pragma mark - 视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.mapView.delegate = self;
    self.locationService.delegate = self;
    self.routeSearch.delegate = self;
}
#pragma mark - 视图将要消失
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.mapView.delegate = nil;
    self.locationService.delegate = nil;
    self.routeSearch.delegate = nil;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 获取资源图片
- (NSString*)getMyBundlePath1:(NSString *)filename {
    
    NSBundle * libBundle = MYBUNDLE ;
    if ( libBundle && filename ){
        NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename];
        return s;
    }
    return nil ;
}
#pragma mark - 公交搜索结果的代理
- (void)onGetTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKTransitRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:self.busPlan];
        self.busPlan ++;
        if (self.busPlan == result.routes.count) {
            self.busPlan = 0;
        }
        // 计算路线方案中的路段数目
        NSInteger size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKTransitStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.instruction;
            item.type = 3;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKTransitStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    } else {
        [self annotation];
        [self busAlertWithError:error];

    }
}
#pragma mark - 自驾搜索结果代理
- (void)onGetDrivingRouteResult:(BMKRouteSearch *)searcher result:(BMKDrivingRouteResult *)result errorCode:(BMKSearchErrorCode)error {
    NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
    [_mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:_mapView.overlays];
    [_mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:self.carPlan];
        self.carPlan ++;
        if (self.carPlan == result.routes.count) {
            self.carPlan = 0;
        }
        // 计算路线方案中的路段数目
        int size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            //添加annotation节点
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.entraceInstruction;
            item.degree = transitStep.direction * 30;
            item.type = 4;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    } else {
        [self annotation];
        [self busAlertWithError:error];
    }
    
}
#pragma mark - 步行搜索结果的代理
- (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error
{
    NSArray* array = [NSArray arrayWithArray:self.mapView.annotations];
    [self.mapView removeAnnotations:array];
    array = [NSArray arrayWithArray:self.mapView.overlays];
    [self.mapView removeOverlays:array];
    if (error == BMK_SEARCH_NO_ERROR) {
        
        BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:self.walkPlan];
        self.walkPlan ++;
        if (self.walkPlan == result.routes.count) {
            self.walkPlan = 0;
        }
        int size = [plan.steps count];
        int planPointCounts = 0;
        for (int i = 0; i < size; i++) {
            BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i];
            if(i==0){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.starting.location;
                item.title = @"起点";
                item.type = 0;
                [_mapView addAnnotation:item]; // 添加起点标注
                
            }else if(i==size-1){
                RouteAnnotation* item = [[RouteAnnotation alloc]init];
                item.coordinate = plan.terminal.location;
                item.title = @"终点";
                item.type = 1;
                [_mapView addAnnotation:item]; // 添加起点标注
            }
            //添加annotation节点
            RouteAnnotation* item = [[RouteAnnotation alloc]init];
            item.coordinate = transitStep.entrace.location;
            item.title = transitStep.entraceInstruction;
            item.degree = transitStep.direction * 30;
            item.type = 4;
            [_mapView addAnnotation:item];
            
            //轨迹点总数累计
            planPointCounts += transitStep.pointsCount;
        }
        
        //轨迹点
        BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
        int i = 0;
        for (int j = 0; j < size; j++) {
            BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j];
            int k=0;
            for(k=0;k<transitStep.pointsCount;k++) {
                temppoints[i].x = transitStep.points[k].x;
                temppoints[i].y = transitStep.points[k].y;
                i++;
            }
            
        }
        // 通过points构建BMKPolyline
        BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
        [_mapView addOverlay:polyLine]; // 添加路线overlay
        delete []temppoints;
        [self mapViewFitPolyLine:polyLine];
    } else {
        [self annotation];
        [self busAlertWithError:error];
    }
}
#pragma mark - 根据polyline设置地图范围
- (void)mapViewFitPolyLine:(BMKPolyline *) polyLine {
    CGFloat ltX, ltY, rbX, rbY;
    if (polyLine.pointCount < 1) {
        return;
    }
    BMKMapPoint pt = polyLine.points[0];
    ltX = pt.x, ltY = pt.y;
    rbX = pt.x, rbY = pt.y;
    for (int i = 1; i < polyLine.pointCount; i++) {
        BMKMapPoint pt = polyLine.points[i];
        if (pt.x < ltX) {
            ltX = pt.x;
        }
        if (pt.x > rbX) {
            rbX = pt.x;
        }
        if (pt.y > ltY) {
            ltY = pt.y;
        }
        if (pt.y < rbY) {
            rbY = pt.y;
        }
    }
    BMKMapRect rect;
    rect.origin = BMKMapPointMake(ltX , ltY);
    rect.size = BMKMapSizeMake(rbX - ltX, rbY - ltY);
    [_mapView setVisibleMapRect:rect];
    _mapView.zoomLevel = _mapView.zoomLevel - 0.3;
}
#pragma mark - 没有搜索到结果的提示框
- (void)busAlertWithError:(int)error {
  
    UIAlertAction *actionSure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    switch (error) {
        case 3:{
            UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"" message:@"该城市不支持公交搜索" preferredStyle:UIAlertControllerStyleAlert];
            [alert1 addAction:actionSure];
            [self presentViewController:alert1 animated:1 completion:nil];
            break;}
        case 4:{
            UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"" message:@"不支持跨城市公交" preferredStyle:UIAlertControllerStyleAlert];
            [alert2 addAction:actionSure];
            [self presentViewController:alert2 animated:1 completion:nil];
            break;}
        case 5:{
            UIAlertController *alert3 = [UIAlertController alertControllerWithTitle:@"" message:@"没有找到检索结果" preferredStyle:UIAlertControllerStyleAlert];
            [alert3 addAction:actionSure];
            [self presentViewController:alert3 animated:1 completion:nil];
            break;}
        case 6:{
            UIAlertController *alert4 = [UIAlertController alertControllerWithTitle:@"" message:@"起终点太近" preferredStyle:UIAlertControllerStyleAlert];
            
            [alert4 addAction:actionSure];
            [self presentViewController:alert4 animated:1 completion:nil];
            break;}
        case 7:{
            UIAlertController *alert5 = [UIAlertController alertControllerWithTitle:@"" message:@"网络连接错误" preferredStyle:UIAlertControllerStyleAlert];
            [alert5 addAction:actionSure];
            [self presentViewController:alert5 animated:1 completion:nil];
            break;}
        case 8:{
            UIAlertController *alert6 = [UIAlertController alertControllerWithTitle:@"" message:@"网络连接超时" preferredStyle:UIAlertControllerStyleAlert];
            [alert6 addAction:actionSure];
            [self presentViewController:alert6 animated:1 completion:nil];
            break;}
        case 9:{
            UIAlertController *alert7 = [UIAlertController alertControllerWithTitle:@"" message:@"未授权" preferredStyle:UIAlertControllerStyleAlert];
            [alert7 addAction:actionSure];
            [self presentViewController:alert7 animated:1 completion:nil];
            break;}
        default:
            break;
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 确定每个点的图片和位置信息
- (BMKAnnotationView*)getRouteAnnotationView:(BMKMapView *)mapview viewForAnnotation:(RouteAnnotation*)routeAnnotation
{
    BMKAnnotationView* view = nil;
    switch (routeAnnotation.type) {
        case 0:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"start_node"];
                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_start.png"]];
                view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 1:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"end_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"end_node"];
                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_end.png"]];
                view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 2:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"bus_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"bus_node"];
                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_bus.png"]];
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 3:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"rail_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"rail_node"];
                view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_rail.png"]];
                view.canShowCallout = TRUE;
            }
            view.annotation = routeAnnotation;
        }
            break;
        case 4:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"route_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"route_node"];
                view.canShowCallout = TRUE;
            } else {
                [view setNeedsDisplay];
            }
            
            UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_direction.png"]];
            view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
            view.annotation = routeAnnotation;
            
        }
            break;
        case 5:
        {
            view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"waypoint_node"];
            if (view == nil) {
                view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"waypoint_node"];
                view.canShowCallout = TRUE;
            } else {
                [view setNeedsDisplay];
            }
            
            UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_waypoint.png"]];
            view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
            view.annotation = routeAnnotation;
        }
            break;
        default:
            break;
    }
    
    return view;
}
#pragma mark - 根据覆盖物画出折线
- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id<BMKOverlay>)overlay
{
    if ([overlay isKindOfClass:[BMKPolyline class]]) {
        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
        polylineView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:1];
        polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
        polylineView.lineWidth = 3.0;
        return polylineView;
    }
    return nil;
}
@end
